const { Sequelize } = require('sequelize');
const path = require('path');
const sqlite3 = require('sqlite3').verbose();

const sequelize = new Sequelize({
  storage: path.join(__dirname, '../db/simple_blog.sqlite'),
  dialect: 'sqlite',
  host: 'localhost',
  dialectModule: sqlite3,
  dialectOptions: {
    dateStrings: true,
    typeCast: true
  },
  define: {
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    freezeTableName: true
  }
});

const checkConnection = async () => {
  try {
    await sequelize.authenticate();
    console.log(`DB Connected`);
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
};

checkConnection();
sequelize.sync({ force: false });

module.exports = sequelize;
